﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZWL.BLL;

namespace YZT.Common
{
    public partial class ProjectAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // ZWL.Common.PublicMethod.CheckSession();
                txtCreateTime.Text = DateTime.Now.ToShortDateString();
                txtCreateUser.Text = ZWL.Common.PublicMethod.GetSessionValue("UserName");
                //设置上传的附件为空
                ZWL.Common.PublicMethod.SetSessionValue("WenJianList", "");
                try
                {
                    //绑定所有表单   限制使用人
                    ZWL.DBUtility.DbHelperSQL.BindDropDownList("select distinct FormName from ERPWorkFlow where FormName in(select FormName from ERPForm where ShiYongUserList='' or ','+ShiYongUserList+',' like '%," + ZWL.Common.PublicMethod.GetSessionValue("UserName") + ",%') order by FormName asc", this.DropDownList1, "FormName", "FormName");


                    //绑定该表单下的所有工作流    限制使用人
                    ZWL.DBUtility.DbHelperSQL.BindDropDownList("select ID,WorkFlowName from ERPWorkFlow where (UserList='' and  FormName='" + this.DropDownList1.SelectedItem.Text + "') or (','+UserList+',' like '%," + ZWL.Common.PublicMethod.GetSessionValue("UserName") + ",%' and  FormName='" + this.DropDownList1.SelectedItem.Text + "') order by WorkFlowName asc", this.DropDownList2, "WorkFlowName", "ID");

                    //绑定工作名称
                    //  this.TextBox1.Text = ZWL.Common.PublicMethod.GetSessionValue("UserName") + "--" + this.DropDownList2.SelectedItem.Text + "(" + DateTime.Now.ToShortDateString() + ")";

                    //绑定下一节点
                    string[] NextStrList = ZWL.DBUtility.DbHelperSQL.GetSHSL("select NextJieDianSerils from ERPWorkFlowJieDian where WorkFlowID=" + this.DropDownList2.SelectedItem.Value.ToString() + " and JieDianAddr='开始'").Split(',');
                    for (int i = 0; i < NextStrList.Length; i++)
                    {
                        ListItem MyItem = new ListItem();
                        MyItem.Value = ZWL.DBUtility.DbHelperSQL.GetSHSL("select ID from ERPWorkFlowJieDian where JieDianSerils='" + NextStrList[i].ToString() + "' and WorkFlowID=" + this.DropDownList2.SelectedItem.Value.ToString());//根据序号和workflowID获得节点ID
                        MyItem.Text = "节点序号：" + NextStrList[i].ToString() + "--节点名称：" + ZWL.DBUtility.DbHelperSQL.GetSHSL("select JieDianName from ERPWorkFlowJieDian where JieDianSerils='" + NextStrList[i].ToString() + "' and WorkFlowID=" + this.DropDownList2.SelectedItem.Value.ToString());
                        if (MyItem.Value.ToString().Length > 0)
                        {
                            //this.DropDownList3.Items.Add(MyItem);
                        }
                    }
                }
                catch
                { }
            }
        }

        protected bool BiaoJiaoTwoStr(string Str1, string Str2, string BiJiaoTiaoJian, string LeiXing)
        {
            if (LeiXing.Trim() == "[数字型]")
            {
                int A1 = int.Parse(Str1);
                int A2 = int.Parse(Str2); //大于  大于等于   小于  小于等于   等于   不等于  包含  不包含
                if (BiJiaoTiaoJian == "大于" && A1 > A2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "大于等于" && A1 >= A2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "小于" && A1 < A2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "小于等于" && A1 <= A2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "等于" && A1 == A2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "不等于" && A1 != A2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "包含" && ZWL.Common.PublicMethod.StrIFIn(Str2, Str1))
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "不包含")
                {
                    if (ZWL.Common.PublicMethod.StrIFIn(Str2, Str1))
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (BiJiaoTiaoJian == "等于" && Str1 == Str2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "不等于" && Str1 != Str2)
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "包含" && ZWL.Common.PublicMethod.StrIFIn(Str2, Str1))
                {
                    return true;
                }
                else if (BiJiaoTiaoJian == "不包含")
                {
                    if (ZWL.Common.PublicMethod.StrIFIn(Str2, Str1))
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
        //判断条件，返回下一节点ID
        protected string CheckTiaoJian(string FormCcontent, string TiaoJianStr, string WorkFlowIDStr)
        {
            //条件格式如：请假天数[数字型]'大于'10→3
            //一：判断FormCcontent中是否包含字段 请假天数[数字型]
            string[] TiaoJianSplit = TiaoJianStr.Split('\'');

            string ZiDuanStr = TiaoJianSplit[0].Split('[')[0]; //字段名称 如：请假天数
            string LeiXingStr = "[" + TiaoJianSplit[0].Split('[')[1]; //字段类型 如：[数字型]
            string BiJiaoStr = TiaoJianSplit[1]; //条件比较  如：大于
            string ZhiStr = TiaoJianSplit[2].Split('→')[0];//比较的值，如： 10
            string JieDianXuHaoStr = TiaoJianSplit[2].Split('→')[1];//跳转的节点序号，如： 3

            //获取 name=TIaoJianZiDuan_请假部门  这个字符串在表单内容中的位置，从该位置向前选30字符，按照 value=  分隔，最后一个字符数组即为值
            int StrIndex = FormCcontent.IndexOf("name=TIaoJianZiDuan_" + ZiDuanStr);
            if (StrIndex > 0)
            {
                string GetSubString = FormCcontent.Substring(StrIndex - 30, 30);
                int ValueIndex = GetSubString.IndexOf("value=");
                if (ValueIndex >= 0)
                {
                    string ValueStr = GetSubString.Substring(ValueIndex + 6, GetSubString.Length - ValueIndex - 6).Trim();
                    if (BiaoJiaoTwoStr(ValueStr, ZhiStr.Trim(), BiJiaoStr, LeiXingStr) == true)
                    {
                        return ZWL.DBUtility.DbHelperSQL.GetSHSLInt("select top 1 ID from ERPWorkFlowJieDian where JieDianSerils='" + JieDianXuHaoStr + "' and WorkFlowID=" + WorkFlowIDStr);
                    }
                }
            }
            return "0";
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //ZWL.BLL.ERPWorkToDo Model = new ZWL.BLL.ERPWorkToDo();
            //Model.FormName = this.DropDownList1.SelectedItem.Text;
            //Model.FuJianList = ZWL.Common.PublicMethod.GetSessionValue("WenJianList");
            //string JingBanRenStr = "";//条件判断时读取经办人
            //Model.ShenPiYiJian = "";

            //Model.TimeStr = DateTime.Now;
            //Model.TongGuoRenList = "";
            //Model.UserName = ZWL.Common.PublicMethod.GetSessionValue("UserName");
            //Model.WorkFlowID = int.Parse(this.DropDownList2.SelectedItem.Value.ToString());
            //Model.WorkName = this.txtPrjName.Text;
            //Model.Add();
            ////写系统日志
            //ZWL.BLL.ERPRiZhi MyRiZhi = new ZWL.BLL.ERPRiZhi();
            //MyRiZhi.UserName = ZWL.Common.PublicMethod.GetSessionValue("UserName");
            //MyRiZhi.DoSomething = "用户添加新工作信息(" + this.txtPrjName.Text + ")";
            //MyRiZhi.IpStr = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
            //MyRiZhi.Add();
            //ZWL.Common.MessageBox.ShowAndRedirect(this, "新工作添加成功！", "MyWorkFlow.aspx");

            ERPXMLB xm = new ERPXMLB();
            xm.FormName = this.DropDownList1.SelectedItem.Text;
            xm.FormID = int.Parse(Helper.CommonHelper.GetFieldValueBySQL("erpform", "id", "FormName='" + xm.FormName + "'"));
            xm.ChuangJianRen = txtCreateUser.Text;
            xm.ChuangJianSJ = DateTime.Parse(txtCreateTime.Text);
            xm.DangQianUser = txtCreateUser.Text;
            xm.PROJECT_ID = Guid.NewGuid().ToString();
            xm.PROJECT_NAME = txtPrjName.Text;
            xm.WorkFlowName = this.DropDownList2.SelectedItem.Text;
            xm.WorkFlowID = int.Parse(this.DropDownList2.SelectedItem.Value.ToString());
            ZWL.BLL.ERPWorkFlowJieDian jd = Helper.WorkFlowHelper.GetStartJieDian((int)xm.WorkFlowID);
            xm.JieDianSerils =jd.JieDianSerils;
            xm.JieDianName = jd.JieDianName;
            xm.SFKBL = 1;
            xm.YXBS = 1;
            xm.Add();


            ZWL.BLL.ERPRiZhi MyRiZhi = new ZWL.BLL.ERPRiZhi();
            MyRiZhi.UserName = ZWL.Common.PublicMethod.GetSessionValue("UserName");
            MyRiZhi.DoSomething = "用户添加新工作信息(" + this.txtPrjName.Text + ")";
            MyRiZhi.IpStr = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
            MyRiZhi.Add();
            ZWL.Common.MessageBox.ShowAndRedirect(this, "新项目添加成功！", "ProjectListInner.aspx");
        }
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
        }
        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
        {
            try
            {

            }
            catch
            { }
        }
        protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
        {
            try
            {

            }
            catch
            { }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DropDownList2.Items.Clear();
            // this.DropDownList3.Items.Clear();



            //绑定该表单下的所有工作流    限制使用人
            ZWL.DBUtility.DbHelperSQL.BindDropDownList("select ID,WorkFlowName from ERPWorkFlow where (UserList='' and  FormName='" + this.DropDownList1.SelectedItem.Text + "') or (','+UserList+',' like '%," + ZWL.Common.PublicMethod.GetSessionValue("UserName") + ",%' and  FormName='" + this.DropDownList1.SelectedItem.Text + "')  order by WorkFlowName asc", this.DropDownList2, "WorkFlowName", "ID");

            try
            {
                //绑定工作名称
                // this.TextBox1.Text = ZWL.Common.PublicMethod.GetSessionValue("UserName") + "--" + this.DropDownList2.SelectedItem.Text + "(" + DateTime.Now.ToShortDateString() + ")";

                //绑定下一节点
                string[] NextStrList = ZWL.DBUtility.DbHelperSQL.GetSHSL("select NextJieDianSerils from ERPWorkFlowJieDian where WorkFlowID=" + this.DropDownList2.SelectedItem.Value.ToString() + " and JieDianAddr='开始'").Split(',');
                for (int i = 0; i < NextStrList.Length; i++)
                {
                    ListItem MyItem = new ListItem();
                    MyItem.Value = ZWL.DBUtility.DbHelperSQL.GetSHSL("select ID from ERPWorkFlowJieDian where JieDianSerils='" + NextStrList[i].ToString() + "' and WorkFlowID=" + this.DropDownList2.SelectedItem.Value.ToString());//根据序号和workflowID获得节点ID
                    MyItem.Text = "节点序号：" + NextStrList[i].ToString() + "--节点名称：" + ZWL.DBUtility.DbHelperSQL.GetSHSL("select JieDianName from ERPWorkFlowJieDian where JieDianSerils='" + NextStrList[i].ToString() + "' and WorkFlowID=" + this.DropDownList2.SelectedItem.Value.ToString());

                }
                //根据选择的节点，绑定人员等信息。
                ZWL.BLL.ERPWorkFlowJieDian MyJieDian = new ZWL.BLL.ERPWorkFlowJieDian();


            }
            catch
            { }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}